export var config = {
  type: Phaser.AUTO,
  width: 800,
  height: 600,
  parent: "phaser-game",
  physics: {
    default: 'arcade',
    arcade: {
      gravity: {
        y: 300
      },
      debug: false
    }
  },
  scene: {
    preload: preload,
    create: create,
    update: update
  }
}

var platforms;
var cursors;
var player;
var stars;
var score = 0;
var scoreText;
var bombs;
let gameOver;


// 预加载
function preload() {
  this.load.image('sky', require('./assets/sky.png'));
  this.load.image('ground', require('./assets/platform.png'));
  this.load.image('star', require('./assets/star.png'));
  this.load.image('bomb', require('./assets/bomb.png'));
  this.load.spritesheet('dude', require('./assets/dude.png'), {
    frameWidth: 32,
    frameHeight: 48
  });

}

// 创建
function create() {
  this.add.image(400, 300, 'sky');

  platforms = this.physics.add.staticGroup(); // 添加物理静态组
  // 放置 4个地面
  platforms.create(400, 568, 'ground').setScale(2).refreshBody();
  platforms.create(600, 400, 'ground');
  platforms.create(50, 250, 'ground');
  platforms.create(750, 220, 'ground');

  // 创建角色
  player = this.physics.add.sprite(100, 450, 'dude')
  player.setBounce(0.2) // 反弹值
  player.setCollideWorldBounds(true) // 限制在当前画面 防止跑出

  // 创建动作帧
  this.anims.create({
    key: 'left',
    frames: this.anims.generateFrameNumbers('dude', {
      start: 0,
      end: 3
    }),
    frameRate: 10, // 每秒10帧
    repeat: -1
  })
  this.anims.create({
    key: 'turn',
    frames: [{
      key: 'dude',
      frame: 4
    }],
    frameRate: 20
  })
  this.anims.create({
    key: 'right',
    frames: this.anims.generateFrameNumbers('dude', {
      start: 5,
      end: 8
    }),
    frameRate: 10,
    repeat: -1
  })

  // player.body.setGravityY(100) // 添加y轴 重力值
  this.physics.add.collider(player, platforms) // 碰撞器

  cursors = this.input.keyboard.createCursorKeys() // 创建键盘管理器

  // 生成组
  stars = this.physics.add.group({
    key: 'star',
    repeat: 11, // 重复11
    setXY: {
      x: 12,
      y: 0,
      stepX: 70
    }
  })
  // 循环 添加y轴反弹值 随机0.4-0.8
  stars.children.iterate(function (child) {
    child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8))
  })

  this.physics.add.collider(stars, platforms) // 加入地面碰撞器

  // 添加 重叠判断 委托
  this.physics.add.overlap(player, stars, collectStar, null, this)

  // 玩家吃到星星后
  function collectStar(player,star) {
    star.disableBody(true, true) // 星星消失
    score += 10
    scoreText.setText('Score:' + score)

    // 判断星星是否全部存活
    if (stars.countActive(true) === 0) {
      stars.children.iterate(function (child) {
        child.enableBody(true,child.x,0,true,true) // 重新激活星星 并设置y=0
      })
      let x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400)
      let bomb = bombs.create(x, 16, 'bomb')
      bomb.setBounce(1)
      bomb.setCollideWorldBounds(true)
      bomb.setVelocityY(Phaser.Math.Between(-200,200),20)
    }
  }

  // 积分系统
  scoreText = this.add.text(16, 16, 'Score:0', { fontSize: '32px', fill: '#000' })

  bombs = this.physics.add.group()
  this.physics.add.collider(bombs, platforms)

  // 碰撞 委托函数
  this.physics.add.collider(player, bombs, hitBomb, null, this)

  function hitBomb(player,bomb) {
    this.physics.pause() // gameover
    player.setTint(0xff0000) // 变红魔法
    player.anims.play('turn')
    gameOver = true
  }
}

// 更新
export function update() {
  // 设置 方向键
  if (gameOver) {
    return
  }

  if (cursors.left.isDown) {
    player.setVelocityX(-160) // x轴速度
    player.anims.play('left', true) // 执行动画帧 循环
  } else if (cursors.right.isDown) {
    player.setVelocityX(160)
    player.anims.play('right', true)
  } else {
    player.setVelocityX(0)
    player.anims.play('turn')
  }
  if (cursors.up.isDown && player.body.touching.down) {
    player.setVelocityY(-330)
  }
}
